package com.zisu.fastdfs.controller;

import com.zisu.fastdfs.utils.FastDFSClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
public class FileController {

    @Autowired
    private FastDFSClientUtils fileDfsUtil;
    /**
     * 文件上传
     * postman 操作：
     *   localhost:8080/uploadFile  ，，选择body ==》 form-data ==》key =file value 选file
     *
     */
    @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
    public R uploadFile (@RequestParam("file") MultipartFile file){
        String result ;
        try{
            String path = fileDfsUtil.upload(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = "上传失败" ;
            }
        } catch (Exception e){
            e.printStackTrace() ;
            result = "服务异常" ;
        }
        return R.ok(result);
    }
    /**
     * 文件删除
     * localhost:8080/deleteByPath?url=group1/M00/00/00/wKgAyV7gQOWALnwQAAVzOIqoR50212.png
     */
    @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
    public R deleteByPath (String url){
        String filePathName = url ;
        fileDfsUtil.deleteFile(filePathName);
        return R.ok("SUCCESS") ;
    }

    /**
     * localhost:8080/downloadFile?url=group1/M00/00/00/wKgAyV7gMRqAcxbQAAAeIwy5Pjs884_big.png
     * */
    @GetMapping("/downloadFile")
    public R downloadFile(String url){
        String g = url;
        byte[] bytes = fileDfsUtil.downloadFile(g,"E:\\ddw.png");
        System.out.println(bytes);
        return R.ok("SUCCESS");
    }
}
